package shixun;

import java.util.*;

public class Formula2 {
    private int num1;
    private int num2;
    private String symbol;

    public Formula2(int num1, String symbol, int num2) {
        this.num1 = num1;
        this.symbol = symbol;
        this.num2 = num2;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Formula2 formula = (Formula2) o;
        return num1 == formula.num1 && num2 == formula.num2 && symbol.equals(formula.symbol);
    }

    @Override
    public int hashCode() {
        return Objects.hash(num1, num2, symbol);
    }

    @Override
    public String toString() {
        return num1 + " " + symbol + " " + num2;
    }

    public int getAnswer() {
        return symbol.equals("+") ? num1 + num2 : num1 - num2;
    }
}

class MathExercises2 {
    private Set<Formula> formulas;
    private Random random;

    public MathExercises2() {
        this.formulas = new LinkedHashSet<>(); // 用LinkedHashSet以保证插入顺序
        this.random = new Random();
    }

    public void addQuestion(String symbol) {
        while (true) {
            int num1 = random.nextInt(101);
            int num2 = random.nextInt(101-num1);
            if (symbol.equals("+") || (num1 > num2 && symbol.equals("-"))) {
                Formula formula = new Formula(num1, symbol, num2);
                if (formulas.add(formula)) {
                    return;
                }
            }
        }
    }

    public void getAssiss(int count) {
        for (int i = 0; i < count; i++) {
            addQuestion((i % 2 == 0) ? "+" : "-");
        }
    }

    public List<String> getQuestion() {
        List<String> exercises = new ArrayList<>();
        getAssiss(50);
        for (Formula f : formulas) {
            exercises.add(f.toString());
        }
        return exercises;
    }

    public List<Integer> getAnswer() {
        List<Integer> answers = new ArrayList<>();
        for (Formula f : formulas) {
            answers.add(f.getAnswer());
        }
        return answers;
    }

    public static void main(String[] args) {
        MathExercises2 exercises = new MathExercises2();
        List<String> questions = exercises.getQuestion();
        List<Integer> answers = exercises.getAnswer();

        Scanner sc = new Scanner(System.in);
        int []a = {5,6,7,23,24,25,59,60,61,119,120};
        System.out.println("题目库:");
        // 按每行6个算式打印输出
        for (int i = 0; i < questions.size(); i++) {
            System.out.printf("%-15s", questions.get(i)); // 使每个算式宽度一致
            if ((i + 1) % 6 == 0) {
                System.out.println(); // 每6个换行
            }
        }



        System.out.println("\n--------------------");
        System.out.println("开始答题:");

        int index = 0;
        while (index < questions.size()) {
            System.out.println("第 " + (index + 1) + " 题: " + questions.get(index));
            System.out.print("请输入答案: ");

            while (!sc.hasNextInt()) {
                System.out.println("请输入一个有效的数字!");
                sc.next(); // 清除无效输入
            }

            int userAnswer = sc.nextInt();
            int correctAnswer = answers.get(index);

            if (userAnswer == correctAnswer) {
                System.out.println("正确!");
            } else {
                System.out.println("错误! 正确答案是: " + correctAnswer);
            }

            index++;
        }

        System.out.println("所有题目已完成，感谢参与!");
        sc.close();
    }
}
